import CommonAction from '../../../actions/common';

let formKeyMap = {};
export default class FormEditAction extends CommonAction {
    constructor(child, comp) {
        super();
        this.comp = comp;
    }

     // 表单编辑前
     formBeforeEdit = async (platFormProps, formId, key, value, data) => {
        const {props} = this.comp;
        const {emp, dispatch, form, meta} = props;
        
        let formData = form.getAllFormValue(formId);

        try {
            let res = await dispatch({
                type: 'emp/formEditBefore',
                payload: {
                    postData: {
                        key: key,
                        pk_org: emp.orgValue.refpk,
                        formData: formData
                    }
                    
                }
            });

            let template = meta.getMeta();

            let formList = this.deepCopy(template['formrelation'][formId]);
            formList.push(formId);

            if(res.data && res.data.refParam) {
                let currentFormId = '';
                if(!formKeyMap[key]) {
                    formList.map((formKey) => {
                        template[formKey].items.map((item) => {
                            if(formKeyMap[formId] === undefined) {
                                formKeyMap[formId] = {};
                            }
                            formKeyMap[formId][item.attrcode] = formKey;
                        });
                    });
                }
                currentFormId = formKeyMap[formId][key];
                if(currentFormId) {
                    template[currentFormId].items.map((item) => {
                        if(item.attrcode === key) {
                            Object.assign(item.queryCondition, res.data.refParam);
                        }
                    });
                    // 因为refer的querycondition从props里拿
                    // 但是这里没有真正更新refer的props
                    // 所以触发一下更新
                    dispatch({
                        type: 'emp/update',
                        payload: {}
                    });
                }
            }
        }
        catch(e) {
            console.error(e);
        }
        return true;
    }

    // 表单编辑后
    formAfterEdit = (platFormProps, formId, key, value, preVal, ...args) => {
        const {props} = this.comp;
        const {emp, dispatch, form, meta} = props;

        let formData = form.getAllFormValue(formId);

        let postData = {
            key: key,
            formData: formData,
            pk_org: emp.orgValue.refpk
        };


        dispatch({
            type: 'emp/formEditAfter',
            payload: {
                postData: postData
            }
        })
            .then((res) => {
                if(res.data) {
                    let {formData, disable, required, visible} = res.data;

                    setTimeout(() => {
                        form.setFormItemsDisabled(formId, {
                            ...disable
                        });
                    }, 1);
                    setTimeout(() => {
                        form.setFormItemsRequired(formId, {
                            ...required
                        });
                    }, 2);
                    
                    setTimeout(() => {
                        form.setItemsVisible(formId, {
                            ...visible
                        });
                    }, 3);

                    if(key === 'pk_org') {
                        formData.head.card.rows[0].values['pk_dept'] = {
                            display: null,
                            value: null
                        };
                    }

                    if(formData && formData.head.card) {
                        setTimeout(() => {
                            console.log(formData);
                            form.setAllFormValue({
                                [formId]: formData.head.card
                            });
                        }, 10);
                    }
                }
            });
    }
}